package com.ict.txmsv2.services;

import com.ict.txmsv2.model.CheckUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author linzzz
 * @desc ...
 * @date 2020-12-16
 */
@Service
public class EmailSendService {

    @Autowired
    TemplateEngine templateEngine;
    @Autowired
    JavaMailSender javaMailSender;

    public void sendThymeleafMail(CheckUser checkUser) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("ICT双创工作室审核通知书");
        helper.setFrom("2377813649@qq.com");
        helper.setTo(checkUser.getEmail());
        helper.setSentDate(new Date());
        Context context = new Context();
        context.setVariable("username", checkUser.getUsername());
        context.setVariable("major",checkUser.getMajor());
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        context.setVariable("date",sdf.format(date));
        context.setVariable("address", checkUser.getAddress());
        String process = templateEngine.process("test.html", context);
        helper.setText(process,true);
        javaMailSender.send(mimeMessage);
    }

}
